function createLoginDiv() {	
    var div = document.createElement("div");
    div.id = "loginForm";
    div.innerHTML = "<h3> &nbsp&nbspLogin</h3><hr/>";
    var div2 = document.createElement("div");
    div2.style.height = "310";
    div2.style.overflow = "auto";
    
    div2.innerHTML = '<form>' + 
                     '&nbsp&nbspAccount: &nbsp&nbsp<input type="text" id="login_account" /><br />' +
                     '&nbsp&nbspPassword: <input type="password" id="login_password" />' +
                     '</form>';
    
    for (var j = 10; j > 0; j--)
		div2.innerHTML = div2.innerHTML + "&nbsp";
    var moreButton = document.createElement('input');
    moreButton.type = 'button';
    moreButton.value = "    login    ";
    moreButton.name = "login";
    moreButton.setAttribute("onclick", "login()");
    div2.appendChild(moreButton);
    var cancelButton = document.createElement('input');
    cancelButton.type = 'button';
    cancelButton.value = "   cancel   ";
    cancelButton.name = "cancelbutton";
    cancelButton.setAttribute("onclick", "cancelLogin()");
    div2.appendChild(cancelButton);
    
	div.appendChild(div2);
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    document.body.appendChild(div);
}

function createRegisterDiv() {
    var div = document.createElement("div");
    div.id = "registerForm";
    div.innerHTML = "<h3> &nbsp&nbspRegister</h3><hr/>";
    var div2 = document.createElement("div");
    div2.style.height = "310";
    div2.style.overflow = "auto";
    
    div2.innerHTML = '<form>' + 
                     '&nbsp&nbspAccount: &nbsp&nbsp<input type="text" id="register_account" /><br />' +
                     '&nbsp&nbspPassword: <input type="password" id="register_password" /><br />' +
                     '&nbsp&nbspConfirm: &nbsp&nbsp<input type="password" id="register_confirm" />' +
                     '</form>';
    
    for (var j = 10; j > 0; j--)
		div2.innerHTML = div2.innerHTML + "&nbsp";
    var moreButton = document.createElement('input');
    moreButton.type = 'button';
    moreButton.value = "    register    ";
    moreButton.name = "register";
    moreButton.setAttribute("onclick", "register()");
    div2.appendChild(moreButton);
    var cancelButton = document.createElement('input');
    cancelButton.type = 'button';
    cancelButton.value = "   cancel   ";
    cancelButton.name = "cancelbutton";
    cancelButton.setAttribute("onclick", "cancelRegister()");
    div2.appendChild(cancelButton);
    
	div.appendChild(div2);
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    div.appendChild(document.createElement('br'));
    document.body.appendChild(div);
}

function login() {
    var account = document.getElementById("login_account").value;
    var password = document.getElementById("login_password").value;
    if (password == '' || account == '')
        alert("Account or Password is empty!");
    else 
	{
		$.post('login.php', 
				{
					'username':account,
					'password':password
				},
				function(data, textStatus, jqXHR)
				{
					console.log(data);
					var loginMsg=data.msg;
					alert(loginMsg);
					fileTreeRefresh();
				},
				'json'
			);
		
	}
    cancelLogin();
}

function register() {
    var account = document.getElementById("register_account").value;
    var password = document.getElementById("register_password").value;
    var confirm = document.getElementById("register_confirm").value;
    if (password != confirm)
        alert("Password Inconsistency!");
    else if (password == '')
        alert("Password cannot be empty!");
    else 
	{
		$.post('reg.php', 
			{
				'username':account,
				'password':password
			},
			function(data, textStatus, jqXHR)
			{
				console.log(data);
				alert(data.msg);
				fileTreeRefresh();
			},
			'json'
		);
	}
    cancelRegister();
}

function cancelLogin() {
	var div = document.getElementById("loginForm");
	document.body.removeChild(div);
}

function cancelRegister() {
	var div = document.getElementById("registerForm");
	document.body.removeChild(div);
}
